/*
 * Copyright 2010,2011 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.schnipsel.swt.baum;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import de.mnit.basis.daten.konstant.QUALITAET;
import de.mnit.basis.daten.struktur.container.Engpass;
import de.mnit.basis.swt.element.feld.baum.SWT_BaumFeld;
import de.mnit.basis.swt.element.feld.menue.SWT_MenueFeldCheck;
import de.mnit.basis.swt.element.menue.SWT_Menue_PopUp;
import de.mnit.basis.swt.grafik.icon.direkt.ICON;
import de.mnit.basis.swt.spezial.SWTX_Baum;
import de.mnit.basis.sys.Ausgabe;
import de.mnit.basis.sys.sprache.S;
import de.mnit.schnipsel.db.DBManager;
import de.mnit.schnipsel.zentral.SC_Bilder;
import de.mnit.schnipsel.zentral.SC_Events;
import de.mnit.schnipsel.zentral.SC_GuiObs;

/**
 * @author Michael Nitsche
 * 23.09.2010	Erstellt
 */
public class BaumPopUp {

	private static final S s=new S(BaumPopUp.class);
	private final Engpass<Point> cursor=Engpass.neu();
	private final SWTX_Baum baum;


	public BaumPopUp(SWTX_Baum baum) {
		this.baum = baum;
	}


	public void init() {
		final SWT_Menue_PopUp dummy=baum.gBaum().nMenu_PopUp();

		//Menü normal
		final SWT_Menue_PopUp menue1=nMenue();

//		menue1.nMenueFeld(s.t("Öffnen")).eAuswahl(new Listener() {
//			public void handleEvent(Event e) {
//				Ausgabe.debug();
//			}
//		});
		menue1.nMenueFeld(s.t("Neu"), null, ICON._16.AKTION_PLUS).eAuswahl(new Listener() {
			public void handleEvent(Event e) {
//				iPlus(baum.gFeld(cursor.g()));
				baum.plus(baum.gFeld(cursor.g()));
			}
		});
		menue1.nMenueFeld(s.t("Entfernen"), null, ICON._16.AKTION_MINUS).eAuswahl(new Listener() {
			public void handleEvent(Event e) {
				baum.minus(baum.gFeld(cursor.g()));
			}
		});

		menue1.nMenueTrenner();
		menue1.nMenueFeld(s.t("Umbenennen"), null, ICON._16.OBJ_NOTIZBLOCK).eAuswahl(new Listener() {
			public void handleEvent(Event e) {
				baum.umbenennen(baum.gFeld(cursor.g()));
			}
		});

		final SWT_MenueFeldCheck mfg=menue1.nMenueFeldCheck(s.t("Geschützt"));
		mfg.sBild(SC_Bilder.gIcon_16_Sicherheit.gIcon());
		mfg.eAuswahl(new Listener() {
			public void handleEvent(Event e) {
				int id=baum.gID(cursor.g());
				boolean geschuetzt=mfg.gAusgewaehlt();
				DBManager.baum().sGeschuetzt(id, geschuetzt);
				SWT_BaumFeld feld=baum.gFeld(cursor.g());
				SC_GuiObs.i().baum().sBild(feld, geschuetzt);
			}
		});
		menue1.eZeigen(new Listener() {
			public void handleEvent(Event e) {
				boolean geschuetzt=DBManager.baum().gGeschuetzt(baum.gID(cursor.g()));
				mfg.sAusgewaehlt(geschuetzt);
			}
		});

		menue1.nMenueTrenner();
		menue1.nMenueFeld(s.t("HTML-Export"), null, ICON._16.OBJ_ERDE).eAuswahl(new Listener() {
			public void handleEvent(Event e) {
				SC_Events.htmlExport(baum.gID(cursor.g()));
			}
		});

		//Menü Funktion
		final SWT_Menue_PopUp menueF=nMenue();

		//Menü Papierkorb
		final SWT_Menue_PopUp menueP=nMenue();
		menueP.nMenueFeld(s.t("Papierkorb leeren")).eAuswahl(new Listener() {
			public void handleEvent(Event e) {
				Ausgabe.debug();
				SC_Events.papierkorbLeeren();
			}
		});

		//Menü Neu
		final SWT_Menue_PopUp menueN=nMenue();

		menueN.nMenueFeld(s.t("Neu"), null, ICON._16.AKTION_PLUS).eAuswahl(new Listener() { // TODO Doppelt!!!!!
			public void handleEvent(Event e) {
//				iPlus(baum.gFeld(cursor.g()));
				baum.plus(baum.gFeld(cursor.g()));
			}
		});


		//Menü-Weiche
		//Bei einem reicht!
		dummy.eZeigen(new Listener() {
			public void handleEvent(Event e) {
				Point p=baum.swt().getDisplay().getCursorLocation();
				cursor.s(p);
				SWT_BaumFeld feld=baum.gFeld(p);
				Integer id= feld==null ? null : baum.gBaum().gID(feld);
//				boolean aktiv= id==null || id>=0;
//				int id=gMenueKlickFeld();

				int wert= id==null || id==BAUMAKTION.ALLE.id || id==BAUMAKTION.LOSE.id ? 1
					: id==BAUMAKTION.MUELL.id ? 2
					: id<0 ? 3
					: 4;
//				Ausgabe.debug(id, wert);

				menueN.swt().setVisible(wert==1);
				menueP.swt().setVisible(wert==2);
				menueF.swt().setVisible(wert==3);
				menue1.swt().setVisible(wert==4);
			}
		});
	}

//	private Integer gMenueKlickFeld() {
//		SWT_BaumFeld feld=baum.gFeld(this.cursor.g());
//		return feld==null ? null : baum.gBaum().gID(feld);
//	}

	private SWT_Menue_PopUp nMenue() {
		SWT_Menue_PopUp popup=new SWT_Menue_PopUp(/*this*/);
		popup.erzeugeSWT(this.baum.swt());
		return popup;
	}
}
